package com.luguotao.lease.web.app.service.impl;



import com.luguotao.lease.common.constant.RedisConstant;
import com.luguotao.lease.common.exception.LeaseException;
import com.luguotao.lease.common.result.ResultCodeEnum;
import com.luguotao.lease.common.utils.CodeUtil;
import com.luguotao.lease.common.utils.JwtUtil;
import com.luguotao.lease.model.entity.UserInfo;
import com.luguotao.lease.model.enums.BaseStatus;
import com.luguotao.lease.web.app.mapper.UserInfoMapper;
import com.luguotao.lease.web.app.service.LoginService;
import com.luguotao.lease.web.app.service.SmsService;
import com.luguotao.lease.web.app.vo.user.LoginVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    SmsService smsService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    UserInfoMapper userInfoMapper;


    @Override
    public void getCode(String phone) {
        //验证码的key
        String codeKey = RedisConstant.APP_LOGIN_PREFIX + phone;
        //判断redis中是否由指定的codekey
        Boolean b = stringRedisTemplate.hasKey(codeKey);
        if (b){//成立则该手机发送过短信
            //获取还剩的时间
            Long expireTime = stringRedisTemplate.getExpire(codeKey);
            //获取验证码的总时间
            Integer totalTime = RedisConstant.APP_LOGIN_CODE_TTL_SEC;
            //判断是否是在一分钟内发布的，是就报错频繁
            if (totalTime - expireTime < RedisConstant.APP_LOGIN_CODE_RESEND_TIME_SEC){
                throw new LeaseException(ResultCodeEnum.APP_SEND_SMS_TOO_OFTEN);
            }
        }


        //生成验证码
        String code = CodeUtil.generateCode(6);
        //发送验证码
        smsService.sendCode(phone,code);
        //保存验证码
        stringRedisTemplate.opsForValue().set(codeKey,code,RedisConstant.APP_LOGIN_CODE_TTL_SEC, TimeUnit.SECONDS);
    }

    //登录
    @Override
    public String login(LoginVo loginVo) {
        String code = loginVo.getCode();
        String phone = loginVo.getPhone();

        //验证码的key
        String codeKey = RedisConstant.APP_LOGIN_PREFIX + phone;

        //手机号不能为空
        if (!StringUtils.hasText(phone)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
        }

        //验证码不能为空
        if (!StringUtils.hasText(code)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }

        //redis验证是否过期
        String redisCode = stringRedisTemplate.opsForValue().get(codeKey);
        if (!StringUtils.hasText(redisCode)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EXPIRED);
        }

        //比较验证码是否一致
        if (!code.equals(redisCode)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }

        //查询数据库是否有对应的手机号信息
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaUpdateWrapper<UserInfo>().eq(UserInfo::getPhone, phone));
        //没有就给他注册
        if (userInfo==null){
            userInfo = new UserInfo();
            userInfo.setPhone(phone);
            userInfo.setStatus(BaseStatus.ENABLE);
            userInfo.setNickname("用户" + phone.substring(7));
            userInfoMapper.insert(userInfo);
        }

        //判断状态是否被禁用
        if (userInfo.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR);
        }

        String token = JwtUtil.creatToken(userInfo.getId(), phone);

        return token;
    }




















}
